package com.systop.asset.service.impl;

import java.util.List;

import com.systop.asset.entity.Asset;
import com.systop.asset.entity.AssetAssignmentsDetail;
import com.systop.asset.entity.Inventory;
import com.systop.asset.entity.vo.AssetAssignmentVO;
import com.systop.asset.entity.vo.ProcessVo;
import com.systop.asset.mapper.AssetAssignmentsDetailMapper;
import com.systop.asset.mapper.AssetMapper;
import com.systop.asset.mapper.InventoryMapper;
import com.systop.asset.service.IProcessService;
import com.systop.asset.utils.RandomCodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.systop.asset.mapper.AssetAssignmentsMapper;
import com.systop.asset.entity.AssetAssignments;
import com.systop.asset.service.IAssetAssignmentsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资产领用记录Service业务层处理
 * 
 * @author mc
 * @date 2025-06-30
 */
@Service
public class AssetAssignmentsServiceImpl implements IAssetAssignmentsService 
{
    @Autowired
    private AssetAssignmentsMapper assetAssignmentsMapper;
    @Autowired
    private IProcessService processService;
    @Autowired
    private AssetAssignmentsDetailMapper assetAssignmentsDetailMapper;
    @Autowired
    private AssetMapper assetMapper;
    @Autowired
    private InventoryMapper inventoryMapper;
    /**
     * 查询资产领用记录
     * 
     * @param id 资产领用记录主键
     * @return 资产领用记录
     */
    @Override
    public AssetAssignments selectAssetAssignmentsById(Long id)
    {
        return assetAssignmentsMapper.selectAssetAssignmentsById(id);
    }

    /**
     * 查询资产领用记录列表
     * 
     * @param assetAssignments 资产领用记录
     * @return 资产领用记录
     */
    @Override
    public List<AssetAssignments> selectAssetAssignmentsList(AssetAssignments assetAssignments)
    {
        return assetAssignmentsMapper.selectAssetAssignmentsList(assetAssignments);
    }

    /**
     * 新增资产领用记录
     * 
     * @param assetAssignments 资产领用记录
     * @return 结果
     */
    @Override
    public int insertAssetAssignments(AssetAssignments assetAssignments)
    {
        List<AssetAssignmentsDetail> assets = assetAssignments.getAssets();
        if(assets!=null){
            //获取登录的用户id
            Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
            assetAssignments.setOperatorId(Long.parseLong(credentials.toString()));
            //assetAssignments.setOperatorId(5L);
            //新增资产领用表数据
            assetAssignmentsMapper.insertAssetAssignments(assetAssignments);
            //新增资产领用明细表数据
            assets.forEach(assetment -> {
                assetment.setAssetsId(assetment.getId());
                assetment.setAssignmentId(assetAssignments.getId());

            });
            assetAssignmentsDetailMapper.insertAssignmentBatch(assets);

            //修改资产表领用状态、资产位置、领用人、使用部门
            for(AssetAssignmentsDetail assetment:assets){
                Asset asset = new Asset();
                asset.setId(assetment.getAssetsId());
                //资产位置
                asset.setLocationId(assetAssignments.getLocationId());
                asset.setDeptId(assetAssignments.getDeptId());
                //通过状态判断当前为领用还是归还
                if(assetAssignments.getUseStatus()!=null && assetAssignments.getUseStatus()==2){
                    //归还
                    asset.setStatus(0);
                    asset.setAssignedTo(null);
                }else{
                    //领用
                    asset.setStatus(2);
                    //领用人
                    asset.setAssignedTo(assetAssignments.getUserId());
                }
                assetMapper.updateAsset(asset);


            }
            return 1;
        }else{
            return 0;
        }

    }

    /**
     * 修改资产领用记录
     * 
     * @param assetAssignments 资产领用记录
     * @return 结果
     */
    @Override
    public int updateAssetAssignments(AssetAssignments assetAssignments)
    {
        return assetAssignmentsMapper.updateAssetAssignments(assetAssignments);
    }

    /**
     * 批量删除资产领用记录
     * 
     * @param ids 需要删除的资产领用记录主键
     * @return 结果
     */
    @Override
    public int deleteAssetAssignmentsByIds(Long[] ids)
    {
        return assetAssignmentsMapper.deleteAssetAssignmentsByIds(ids);
    }

    /**
     * 删除资产领用记录信息
     * 
     * @param id 资产领用记录主键
     * @return 结果
     */
    @Override
    public int deleteAssetAssignmentsById(Long id)
    {
        return assetAssignmentsMapper.deleteAssetAssignmentsById(id);
    }
    @Override
    public AssetAssignmentVO selectDetail(String id){
        return assetAssignmentsMapper.selectDetail(id);
    }
    @Override
    public AssetAssignmentVO selectDetailAll(String id){
        return assetAssignmentsMapper.selectDetailAll(id);
    }

    /**
     * 新增耗材出库领用记录
     * @param assetAssignments
     * @return
     */
    @Override
    @Transactional
    public int insertAssetAssignmentsByMaterial(AssetAssignments assetAssignments) {
        //获取登录的用户id
        Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
        assetAssignments.setOperatorId(Long.parseLong(credentials.toString()));
        assetAssignments.setType(1);
        //新增领用记录
        assetAssignmentsMapper.insertAssetAssignments(assetAssignments);
        //新增领用详情\更新库存
        for (AssetAssignmentsDetail  detail: assetAssignments.getAssets()) {
            //新增领用详情
            detail.setAssignmentId(assetAssignments.getId());
            detail.setAssetCode(RandomCodeGenerator.generateCustomCode("HCCK"));
            assetAssignmentsDetailMapper.insertAssetAssignmentsDetail(detail);
            //更新库存
            Inventory inventory = inventoryMapper.selectInventoryById(detail.getInventoryId());
            inventory.setQuantity(inventory.getQuantity()-detail.getMaterialNum());
            inventoryMapper.updateInventory(inventory);
        }

        return 1;
    }

    @Override
    public AssetAssignmentVO selectDetailByMaterial(String id, Integer type) {
        return assetAssignmentsMapper.selectDetailByMaterial(id,type);
    }

    @Override
    public List<AssetAssignments> selectAssetAssignmentsListByMaterial(AssetAssignments assetAssignments) {
        //查询耗材领用记录列表
        return assetAssignmentsMapper.selectMaterialBorrowList(assetAssignments);
    }

    @Override
    public List<AssetAssignments> materialListByWechat(AssetAssignments assetAssignments) {
        return assetAssignmentsMapper.selectMaterialBorrowListByWechat(assetAssignments);
    }

    /**
     * 新增资产领用记录
     *
     * @param assetAssignments 资产领用记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAssetAssignmentsByWechat(AssetAssignments assetAssignments)
    {
        List<AssetAssignmentsDetail> assets = assetAssignments.getAssets();
        if(assets!=null){
            //获取登录的用户id
            Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
            assetAssignments.setOperatorId(Long.parseLong(credentials.toString()));
            assetAssignments.setUseStatus(4L); //设置使用状态：4审批中
            //assetAssignments.setOperatorId(5L);
            //新增资产领用表数据
            assetAssignmentsMapper.insertAssetAssignments(assetAssignments);
            //新增资产领用明细表数据
            assets.forEach(assetment -> {
                assetment.setAssetsId(assetment.getId());
                assetment.setAssignmentId(assetAssignments.getId());

            });
            assetAssignmentsDetailMapper.insertAssignmentBatch(assets);

            //修改资产表领用状态、领用人、等审核通过之后使用部门\资产位置
            for(AssetAssignmentsDetail assetment:assets){
                Asset asset = new Asset();
                asset.setId(assetment.getAssetsId());
                //资产位置
                //asset.setLocationId(assetAssignments.getLocationId());
                //asset.setDeptId(assetAssignments.getDeptId());
                //通过状态判断当前为领用还是归还
                if(assetAssignments.getUseStatus()!=null && assetAssignments.getUseStatus()==2){
                    //归还
                    asset.setStatus(0);
                    asset.setAssignedTo(null);
                }else{
                    //设备状态：1审批中
                    asset.setStatus(1);
                    //领用人
                    asset.setAssignedTo(assetAssignments.getUserId());
                }
                assetMapper.updateAsset(asset);
            }

            //发起审批流程
            ProcessVo vo = new ProcessVo();
            vo.setUserId(Long.parseLong(credentials.toString())); //申请人
            vo.setDeptId(assetAssignments.getDeptId()); //申请人部门
            vo.setBusinessId(assetAssignments.getId()); //业务id：资产领用记录表id
            vo.setProcessType("资产借用申请"); //审批类型
            //发起审批：资产借用
            processService.publishProcess(vo);

            return 1;
        }else{
            return 0;
        }

    }


}
